*****************************************************************************************

* This file merges the data sets created in the earlier do-files

*****************************************************************************************

/* steps in below file

1. subset the icma that can be matched to nhgis
2. merge nhgis to permits
3. merge in icma and restrict to towns that match at least one icma
4. merge in lihtc

*/


** set directory

* start by loading the ICMA and subsetting those that can be matched to NHGIS
use "icma_all_cleaned_v2.dta",clear
gen placefips=placefips_final
merge m:1 statefips placefips using "nhgis_data.dta",keepus(vac_rate10)
drop vac_rate10

drop if _merge == 2
drop if _merge == 1
drop _merge

isid statefips countyfips placefips year

save "nhgis_icma_v2.dta", replace

**** load permits data and merge on sequentially
use "1980-2018_permits_master_WF_v4.dta", clear

* merge in nhgis data to pair a characteristics with every place year
merge m:1 statefips placefips using "nhgis_data.dta"
keep if _merge == 3
drop _merge

* add icma data
merge 1:1 statefips countyfips placefips year using "nhgis_icma_v2.dta", gen(m3) force
rename id2 SR_id

* drop duplicates contained in permits data
duplicates tag SR_id year, gen(tag)
tab tag
drop if tag != 0
drop tag

* drop variables we don't use
drop PLACE m7 m8 ujuris place_icma place_type_icma q1 pop2000 pop1990 q8b1 q8b2 q8b3 q8b4 q8b5 ///
	pop1980 pop1984 pop1985 drop_name
drop n_yr smp_pct 
drop id_final id  
rename why switch_reason
drop m3 imisid
drop placefips
rename placefips_final placefips

* keep towns that get matched to at least one ICMA
rename SR_id id
tsset id year
gen icma_dummy=icma_region!=""
bys id: egen any_icma_match=max(icma_dummy)
keep if any_icma_match==1
egen town_tag=tag(id)

* add LIHTC data and code permit values
merge 1:1 placefip statefips year using "lihtc_placefips_by_year_allocated_formerge.dta"
drop if _merge==2
replace li_units=0 if _merge==1
replace n_units=0 if _merge==1
replace n_proj=0 if _merge==1
drop _merge

replace li_units=. if year>2015
replace n_units=. if year>2015
replace n_proj=. if year>2015

drop if year == .

save "1980-2018_permits_master_SR.dta", replace
